
package Testing;

import ClientGraphics.GraphicsMain;
import GameEngine.GameConstants.Graphic;
import GameEngine.GameConstants.Levels;
import GameEngine.GameConstants.Thing;
import GameEngine.Level;
import GameEngine.UserAction;
import GameEngine.Vector;
import Objects.BasicObject;
import Objects.GameObject;
import Objects.Player;
import Objects.SpawnPoint;
import Objects.Wall;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
 *
 * @author mhfowler
 */
public class TestLevel {

    /* millisecond between gamestate updates */
    final int MBGU = 100;

    // for graphics
    private static GraphicsMain gMain;
    private static JFrame mainFrame;




/* main method bitches */
   public static void main(String[] args) {
       TestLevel tester = new TestLevel();
        // choose test
       Level toTest = createTestLevel(3);
       System.out.println("level loaded: " + toTest.toString());
       List<BasicObject> toRender1 = toTest.getBasics();
       List<UserAction> parsed = new LinkedList<UserAction>();
       List<BasicObject> toRender2 = toTest.updateState(parsed);
       System.out.println("level updated: " + toTest.toString());

       // now do graphics
        gMain = new GraphicsMain(Levels.ABSTRACT, new Dimension(1000,1500), 1000,1500);
        mainFrame = new JFrame("test");
        mainFrame.setVisible(true);
        mainFrame.setSize(new Dimension(1000,1500));
        mainFrame.setResizable(true);
        mainFrame.add(gMain.getGraphicsPanel());
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel check = gMain.getGraphicsPanel();
        Graphics g = check.getGraphics();
        Graphics2D g2 = (Graphics2D) g;
        System.out.println("check");
        gMain.initialize();
        System.out.println("done construction");
        gMain.update(toRender1);
        // now lets really test
        tester.test(toTest);

   }


      /* The second test showcases two characters moving about 500 pixels, over the course of 200 gamestate
     * updates.
     */
    public void test(Level lev) {
                /* lets fill this list of game states */
                   /* now create timer which calls update every time interval, and pops off the next
                 * game state in the list */
                 Timer time = new Timer(MBGU,null);
                 TestListener tester = new TestListener(gMain, lev, time);
                 time.addActionListener(tester);
                 time.start();
    }



       private class TestListener implements ActionListener {

        GraphicsMain gTest;
        Level lev;
        Timer parent;
        int counter;

        public TestListener(GraphicsMain g,Level l, Timer p) {
            gTest = g;
            lev = l;
            parent = p;
            counter = 0;
        }


        @Override
        public void actionPerformed(ActionEvent e) {
            if (counter <100) {
                List<BasicObject> toRender = lev.updateState(new LinkedList<UserAction>());
                gTest.update(toRender);
                counter++;
            }
            else {
                System.out.println("finito");
                parent.stop();
            }
        }


    }
       
   // creates a list of basic object for testing, using a switch case for different tests
    public static Level createTestLevel(int a) {
        int counter = 0;
        Level lev = null;
        switch (a) {
            case 1:
                    GameObject obj1 = GameEngine.LevelInitializer.objectConstructor(Thing.PLAYER, new Vector(1,0), new Vector (300,300), new Vector(50,50));
                    GameObject obj2 = GameEngine.LevelInitializer.objectConstructor(Thing.WALL, new Vector(1,0), new Vector (300,400), new Vector(500,10));
                    SpawnPoint obj3 = (SpawnPoint) GameEngine.LevelInitializer.objectConstructor(Thing.SPAWNPOINT, new Vector(0,1), new Vector (100,100), new Vector(50,50));
                    GameObject obj7 = GameEngine.LevelInitializer.objectConstructor(Thing.PLAYER, new Vector(1,0), new Vector (600,300), new Vector(50,50));
                    GameObject obj8 = GameEngine.LevelInitializer.objectConstructor(Thing.WALL, new Vector(1,0), new Vector (300,800), new Vector(800,10));
                    LinkedList<GameObject> list = new LinkedList<GameObject>();
                    obj1.setId((short)1);
                    obj2.setId((short)2);
                    obj3.setId((short)3);
                    obj7.setId((short)4);
                    obj8.setId((short)5);
                    list.add(obj1);
                    list.add(obj2);
                    list.add(obj3);
                    list.add(obj7);
                    list.add(obj8);
                    lev = new Level(list, 1000, 1500, 1, obj3);
                break;
            case 2:
                GameObject obj4 = GameEngine.LevelInitializer.objectConstructor(Thing.PLAYER, new Vector(1,0), new Vector (300,300), new Vector(50,50));
                    GameObject obj5 = GameEngine.LevelInitializer.objectConstructor(Thing.WALL, new Vector(1,0), new Vector (300,300), new Vector(200,5));
                    SpawnPoint obj6 = (SpawnPoint) GameEngine.LevelInitializer.objectConstructor(Thing.SPAWNPOINT, new Vector(0,1), new Vector (100,100), new Vector(50,50));
                    LinkedList<GameObject> list2 = new LinkedList<GameObject>();
                    obj4.setId((short)1);
                    obj5.setId((short)2);
                    obj6.setId((short)3);
                    list2.add(obj4);
                    list2.add(obj5);
                    list2.add(obj6);
                    lev = new Level(list2, 1000, 1500, 1, obj6);
            case 3:
                    GameObject obj9 = GameEngine.LevelInitializer.objectConstructor(Thing.PLAYER, new Vector(1,0), new Vector (300,300), new Vector(50,50));
                    GameObject obj10 = GameEngine.LevelInitializer.objectConstructor(Thing.WALL, new Vector(1/Math.sqrt(2),1/Math.sqrt(2)), new Vector (300,400), new Vector(500,10));
                    SpawnPoint obj11 = (SpawnPoint) GameEngine.LevelInitializer.objectConstructor(Thing.SPAWNPOINT, new Vector(0,1), new Vector (100,100), new Vector(50,50));
                    GameObject obj12 = GameEngine.LevelInitializer.objectConstructor(Thing.PLAYER, new Vector(1,0), new Vector (600,300), new Vector(50,50));
                    GameObject obj13 = GameEngine.LevelInitializer.objectConstructor(Thing.WALL, new Vector(-1/Math.sqrt(2),1/Math.sqrt(2)), new Vector (300,800), new Vector(800,10));
                    LinkedList<GameObject> list3 = new LinkedList<GameObject>();
                    obj9.setId((short)1);
                    obj10.setId((short)2);
                    obj11.setId((short)3);
                    obj12.setId((short)4);
                    obj13.setId((short)5);
                    list3.add(obj9);
                    list3.add(obj10);
                    list3.add(obj11);
                    list3.add(obj12);
                    list3.add(obj13);
                    lev = new Level(list3, 1000, 1500, 1, obj11);
                break;
            default:
                break;
        }
        return lev;
    }








}
